********************************************************************************
** 	TITLE:		NZ02_results                                                  ** 
**	AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       nz02                                                          **
**	VERSION:	Stata 16					                                  **
**	DATE:		October 2022  				                                  **
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close
log using "NZ02_results.smcl", replace

* Open the dataset

use "nz02.dta", clear

* Reshape

drop if electoratedistrict == "General Electorate Totals"
drop if electoratedistrict == "Maori Electorate Totals"
drop if electoratedistrict == "Combined Totals"

sort electoratedistrict

gen district_id = _n

rename act votes1
rename alliance votes2
rename legalisecannabis votes3
rename christianheritage votes4
rename greenparty votes5
rename labourparty votes6
rename manamaori votes7
rename nationalparty votes8
rename nzfirst votes9
rename nmp votes10
rename onenzparty votes11
rename unitedfuture votes12
rename outdoorrecnz votes13
rename progressivecoalition votes14

gen share1 = (votes1/totalvalidpartyvotes)*100
gen share2 = (votes2/totalvalidpartyvotes)*100
gen share3 = (votes3/totalvalidpartyvotes)*100
gen share4 = (votes4/totalvalidpartyvotes)*100
gen share5 = (votes5/totalvalidpartyvotes)*100
gen share6 = (votes6/totalvalidpartyvotes)*100
gen share7 = (votes7/totalvalidpartyvotes)*100
gen share8 = (votes8/totalvalidpartyvotes)*100
gen share9 = (votes9/totalvalidpartyvotes)*100
gen share10 = (votes10/totalvalidpartyvotes)*100
gen share11 = (votes11/totalvalidpartyvotes)*100
gen share12 = (votes12/totalvalidpartyvotes)*100
gen share13 = (votes13/totalvalidpartyvotes)*100
gen share14 = (votes14/totalvalidpartyvotes)*100

reshape long votes share, i(district_id) j(party_code)  

gen party_name = " "

replace party_name = "ACT" if party_code == 1 
replace party_name = "Alliance" if party_code == 2
replace party_name = "Legalise Cannabis" if party_code == 3
replace party_name = "Christian Heritage" if party_code == 4
replace party_name = "Green" if party_code == 5 
replace party_name = "Labour" if party_code == 6
replace party_name = "Mana Maori" if party_code == 7
replace party_name = "National" if party_code == 8
replace party_name = "NZ First" if party_code == 9
replace party_name = "NMP" if party_code == 10
replace party_name = "One NZ" if party_code == 11
replace party_name = "United Future" if party_code == 12
replace party_name = "Outdoor Rec. NZ" if party_code == 13
replace party_name = "Progressive Coalition" if party_code == 14

drop party_code

gen party_code = .

replace party_code = 1 if party_name == "ACT"
replace party_code = 2 if party_name == "Alliance"
replace party_code = 3 if party_name == "Christian Heritage"
replace party_code = 4 if party_name == "Green"
replace party_code = 5 if party_name == "Progressive Conservative"
replace party_code = 6 if party_name == "Labour"
replace party_code = 7 if party_name == "National"
replace party_code = 8 if party_name == "NZ First"
replace party_code = 9 if party_name == "United Future"
replace party_code = 37 if party_name!="ACT" & party_name!="Alliance" & party_name!="Christian Heritage" & party_name!="Green" & party_name!="Progressive Conservative" & party_name!="Labour" & party_name!="National" & party_name!="NZ First" & party_name!="United Future"

* Generate rank of parties

gsort electoratedistrict -votes
bysort electoratedistrict : gen rank = _n

gen first = party_name if rank == 1
gen second = party_name if rank == 2
gen third = party_name if rank == 3

bysort electoratedistrict : gen winner = first[1]
bysort electoratedistrict : gen runnerup = second[2]
bysort electoratedistrict : gen thirdplace = third[3]

* Generate margins of victory

bysort electoratedistrict : gen margin = share[1] - share[2]

* Generate effective number of electoral parties

gen sqrvotepr = (share/100)^2

bysort electoratedistrict : egen sumsqrvotepr = sum(sqrvotepr)

gen enep = 1/sumsqrvotepr

* Selecting, ordering, and sorting variables

keep district_id electoratedistrict party_name party_code rank votes share margin enep winner runnerup thirdplace

order district_id electoratedistrict party_name party_code rank votes share margin enep winner runnerup thirdplace

sort district_id rank

drop if rank > 3

* Drop duplicates

duplicates tag district_id, gen(dup)
duplicates drop district_id, force
drop dup

* Create election variable

gen election02 = "2002 New Zealand general election"

* Saving file

save NZ02_results, replace

log close